﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WLG.Model.NotifyContext;
using System.Net;
using System.IO;
using WLG.Model.Interface;
using WLG.NotifyLiveDataProvider.PersonalFeed;
using WLG.Model.Permissions;

namespace WLG.NotifyLiveDataProvider
{
    public class LivePersonalDataProvider : INotifyDataProvider
    {
        public List<Notification> GetNotifications(Identity ident)
        {
            List<Notification> results = new List<Notification>();
            string Url = "http://sup.live.com/whatsnew/rafeed.aspx?cid=" + ident.CID + "&mkt=en-US";

 


            LiveServicesNotifyDS NotifyFeedXML = ScrapeFeed(Url);


            foreach (LiveServicesNotifyDS.entryRow r in NotifyFeedXML.entry)
            {
                Notification n = new Notification();
                results.Add(n);
                n.ID = r.id;
                n.Title = r.title;
                DateTime.TryParse(r.updated, out n.Updated);

                foreach (LiveServicesNotifyDS.summaryRow sr in r.GetsummaryRows())
                {
                    n.Summary = new NotifySummary();
                    n.Summary.SummaryText = sr.summary_Text;
                    n.Summary.Type = sr.type;
                }

                //foreach (LiveServicesNotifyDS.authorRow ar in r.GetauthorRows())
                //{
                //    NotifyAuthor aa = new NotifyAuthor();
                //    aa.Name = ar.name;
                //    n.Authors.Add(aa);
                //}


                foreach (LiveServicesNotifyDS.contentRow cr in r.GetcontentRows ())
                {
                    n.Content = new NotifyContent();
                    n.Content.Corpus = cr.content_Text;
                    n.Content.Type = cr.type;
                }

                foreach (LiveServicesNotifyDS.linkRow lr in r.GetlinkRows() )
                {
                    n.Link = new NotifyLink();
                    n.Link.Href = lr.href;
                    try
                    {
                        if (lr.rel != null)
                            n.Link.Relative = lr.rel;
                    }
                    catch { }
                }
            }

            return results;
        }

        private static LiveServicesNotifyDS ScrapeFeed(string Url)
        {
            LiveServicesNotifyDS result = new LiveServicesNotifyDS();
            HttpWebRequest WebRequestObject = null;
            WebResponse Response = null;
            Stream WebStream = null;
            try
            {

                WebRequestObject = (HttpWebRequest)HttpWebRequest.Create(Url);

                WebRequestObject.UserAgent = ".NET Framework/3.5";
                WebRequestObject.Referer = "http://www.deepelement.com/blog";


                Response = WebRequestObject.GetResponse();
                WebStream = Response.GetResponseStream();
                result.ReadXml(WebStream);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (Response != null)
                    Response.Close();
            }
            return result;
        }
    }
}
